package com.sst.social.networking;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import facebook.DialogError;
import facebook.Facebook;
import facebook.FacebookError;
import facebook.Facebook.DialogListener;

public class ShareOnFacebook extends Activity {
	/** Called when the activity is first created. */
	private static final String APP_ID = "178998655489553";// "153786151325263";
	private static final String[] PERMISSIONS = new String[]{"publish_stream"};

	private static final String TOKEN = "access_token";
	private static final String EXPIRES = "expires_in";
	private static final String KEY = "facebook-credentials";

	private Facebook facebook;
	private String messageToPost;

	// Facebook facebook = new Facebook("178998655489553");

	/*
	 * @Override public void onCreate(Bundle savedInstanceState) {
	 * super.onCreate(savedInstanceState); setContentView(R.layout.main);
	 * 
	 * facebook.authorize(this, new DialogListener() {
	 * 
	 * @Override public void onComplete(Bundle values) {
	 * 
	 * }
	 * 
	 * @Override public void onFacebookError(FacebookError error) {
	 * 
	 * }
	 * 
	 * @Override public void onError(DialogError e) {
	 * 
	 * }
	 * 
	 * @Override public void onCancel() {
	 * 
	 * } }); } }
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		facebook = new Facebook("aiosn");
		restoreCredentials(facebook);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main);

		String facebookMessage = getIntent().getStringExtra("facebookMessage");
		if (facebookMessage == null) {
			facebookMessage = "Test wall post";
		}
		messageToPost = facebookMessage;
		loginAndPostToWall();
	}

	public void doNotShare(View button) {
		finish();
	}

	public void share(View button) {
		if (!facebook.isSessionValid()) {
			loginAndPostToWall();
		} else {
			postToWall(messageToPost);
		}
	}

	public void loginAndPostToWall() {

		facebook.authorize(ShareOnFacebook.this, PERMISSIONS,
				new LoginDialogListener());

	}

	public void postToWall(String message) {
		Bundle parameters = new Bundle();
		parameters.putString("message", message);
		facebook.dialog(this, "stream.publish", parameters,
				new WallPostDialogListener());
	}

	class LoginDialogListener implements DialogListener {
		public void onComplete(Bundle values) {
			saveCredentials(facebook);
			if (messageToPost != null) {
				postToWall(messageToPost);
			}
		}

		public void onFacebookError(FacebookError error) {
			showToast("Authentication with Facebook failed!");
			finish();
		}

		public void onError(DialogError error) {
			showToast("Authentication with Facebook failed!");
			finish();
		}

		public void onCancel() {
			showToast("Authentication with Facebook cancelled!");
			finish();
		}
	}

	class WallPostDialogListener implements DialogListener {
		public void onComplete(Bundle values) {
			final String postId = values.getString("post_id");
			if (postId != null) {
				showToast("Message posted to your facebook wall!");
			} else {
				showToast("Wall post cancelled!");
			}
			finish();
		}

		public void onFacebookError(FacebookError e) {
			showToast("Failed to post to wall!");
			e.printStackTrace();
			finish();
		}

		public void onError(DialogError e) {
			showToast("Failed to post to wall!");
			e.printStackTrace();
			finish();
		}

		public void onCancel() {
			showToast("Wall post cancelled!");
			finish();
		}
	}

	private void showToast(String message) {
		Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
				.show();
	}
	public boolean saveCredentials(Facebook facebook) {
		Editor editor = getApplicationContext().getSharedPreferences(KEY,
				Context.MODE_PRIVATE).edit();
		editor.putString(TOKEN, facebook.getAccessToken());
		editor.putLong(EXPIRES, facebook.getAccessExpires());
		return editor.commit();
	}

	public boolean restoreCredentials(Facebook facebook) {
		SharedPreferences sharedPreferences = getApplicationContext()
				.getSharedPreferences(KEY, Context.MODE_PRIVATE);
		facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
		facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
		return facebook.isSessionValid();
	}

}
